package com.ming.transaction.common.annotation;

import com.ming.transaction.config.RabbitMQConfig;
import com.ming.transaction.entity.Dialy;
import com.ming.transaction.mapper.DialyMapper;
import com.ming.transaction.service.IDialyService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * 切面编写
 * @author: Chenming
 * @date 2023/5/8 15:19
 */

@Aspect
@Component
public class RecordLoggerAcpect {

    ThreadLocal<Long> currentTime = new ThreadLocal<>();

    private final RabbitTemplate rabbitTemplate;

    public RecordLoggerAcpect(RabbitTemplate rabbitTemplate){
        this.rabbitTemplate = rabbitTemplate;
    }

    /**
     * @Description: 定义切入点
     */
    //被注解CustomAopAnnotation表示的方法
    @Pointcut("@annotation(com.ming.transaction.common.annotation.RecordLoggerAnnotation)")
    public void pointCut(){}

    /**
     * @Description: 定义前置通知
     */
    @Before("pointCut()")
    public void before(JoinPoint joinPoint) throws Throwable {

        currentTime.set(System.currentTimeMillis());

        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        Dialy dialy = new Dialy();
        dialy.setStatus("正常");
        dialy.setAddress(request.getRequestURL().toString());
        dialy.setMethod(request.getMethod());
        dialy.setParams(Arrays.toString(joinPoint.getArgs()));
        dialy.setHost(request.getRemoteAddr());
        dialy.setConmethod(joinPoint.getSignature().getName());
        dialy.setUnumber((String) request.getSession().getAttribute("Number"));

        rabbitTemplate.convertAndSend(RabbitMQConfig.ITEM_EXCHANGE,"log.info",dialy);

    }

    /**
     * @Description: 后置异常通知
     */
    @AfterThrowing("pointCut()")
    public void afterThrowing(JoinPoint joinPoint){

        currentTime.set(System.currentTimeMillis());

        Dialy dialy = new Dialy();

        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        dialy.setStatus("异常");
        dialy.setAddress(request.getRequestURL().toString());
        dialy.setMethod(request.getMethod());
        dialy.setParams(Arrays.toString(joinPoint.getArgs()));
        dialy.setHost(request.getRemoteAddr());
        dialy.setConmethod(joinPoint.getSignature().getName());
        dialy.setUnumber((String) request.getSession().getAttribute("Number"));

        rabbitTemplate.convertAndSend(RabbitMQConfig.ITEM_EXCHANGE,"log.info",dialy);
    }

    ///**
    // * @Description: 后置返回通知
    // */
    //@AfterReturning(returning = "ret", pointcut = "pointCut()")
    //public void afterReturning(Object ret) throws Throwable {
    //}

    ///**
    // * @Description: 后置最终通知,final增强，不管是抛出异常或者正常退出都会执行
    // */
    //@After("pointCut()")
    //public void after(JoinPoint jp){
    //}

    ///**
    // * @Description: 环绕通知,环绕增强
    // * @return
    // */
    //@Around("pointCut()")
    //public Object around(ProceedingJoinPoint pjp) {
    //}

}
